//
//  FFWalletVM.m
//  LLWallet
//
//  Created by tt on 2020/8/16.
//

#import "FFWalletVM.h"
#import "FFWalletCardCell.h"
#import "WKLabelItemCell.h"
#import "FFWallItemListCell.h"
#import "FFWalletConst.h"
#import "FFWalletRechargeVC.h"
#import "FFPayPwdUpdateVC.h"
#import "WKPwdKeyboardInputView.h"
#import "FFWalletManager.h"
#import "FFWalletWithdrawAccountVC.h"
#import "FFServicerListVC.h"
#import "FFWithdrawVC.h"
#import "FFPasswordFreePayVC.h"
#import "WKWithdrawBiVC.h"
#import "WKCoinRechargeVC.h"

@implementation FFWalletVM

- (NSArray<NSDictionary *> *)tableSectionMaps {
   NSArray *items = [[WKApp shared] invokes:FFPOINT_CATEGORY_WALLITEM param:nil];
    return @[
        @{
            @"height":@(20.0f),
            @"items":@[
                    @{
                        @"class":FFWalletCardModel.class,
                        @"amount":@(self.walletInfo?self.walletInfo.amount:0),
                        @"onRecharge":^{ // 充值点击
                            __weak typeof(self) weakSelf = self;
                            [self requestType:^(NSError * _Nullable) {
                                
                                WKActionSheetView2 *sheet = [WKActionSheetView2 initWithTip:nil];
                                
                                for (FFRechargeChannel *chanel in weakSelf.config.channels) {
                                    [sheet addItem:[WKActionSheetButtonItem2 initWithTitle:LLang(LLang(chanel.title)) onClick:^{
                                        if (chanel.type == 4){
                                            WKCoinRechargeVC *vc = [WKCoinRechargeVC new];
                                            vc.config = weakSelf.config;
                                            [[WKNavigationManager shared] pushViewController:vc animated:YES];
                                        }else{
                                            FFWalletRechargeVC *vc = [FFWalletRechargeVC new];
                                            vc.viewModel.payType = chanel.type;
                                            [[WKNavigationManager shared] pushViewController:vc animated:YES];
                                        }
                                    }]];
                                }
                                if ( weakSelf.config.channels.count > 0){
                                    [sheet show];
                                }
                            }];
                           
                        },
                        @"onWithdraw":^{ // 提现点击
                            
                            __weak typeof(self) weakSelf = self;
                            WKActionSheetView2 *sheet = [WKActionSheetView2 initWithTip:nil];
                            [sheet addItem:[WKActionSheetButtonItem2 initWithTitle:LLang(LLang(@"银行卡")) onClick:^{
                                [[WKNavigationManager shared] pushViewController:[FFWithdrawVC new] animated:YES];
                            }]];
                            [sheet addItem:[WKActionSheetButtonItem2 initWithTitle:LLang(LLang(@"虚拟货币")) onClick:^{
                                [[WKNavigationManager shared] pushViewController:[WKWithdrawBiVC new] animated:YES];
                            }]];
                            [sheet show];
                        
                        },
                    },
            ]
        },
        @{
            @"height":@(20.0f),
            @"items":@[
                    @{
                        @"class":FFWallItemListModel.class,
                        @"items":items?:@[],
                         @"showArrow":@(false),
                        @"onClick":^{
                            
                        }
                    },
            ]
        },
        @{
             @"height":@(20.0f),
             @"items":@[
                     @{
                         @"class":WKLabelItemModel.class,
                         @"label":LLang(@"支付密码"),
                         @"showBottomLine":@(NO),
                         @"showTopLine":@(NO),
                         @"onClick":^{
                             [[WKNavigationManager shared] pushViewController:[FFPayPwdUpdateVC new] animated:YES];
                            
                         }
                     },
                     @{
                         @"class":WKLabelItemModel.class,
                         @"label":LLang(@"免密支付"),
                         @"showBottomLine":@(NO),
                         @"showTopLine":@(NO),
                         @"onClick":^{
                             [[WKNavigationManager shared] pushViewController:FFPasswordFreePayVC.new animated:YES];
                         }
                     },
                     @{
                         @"class":WKLabelItemModel.class,
                         @"label":LLang(@"提现账户"),
                         @"showBottomLine":@(NO),
                         @"showTopLine":@(NO),
                         @"onClick":^{
                             [[WKNavigationManager shared] pushViewController:[FFWalletWithdrawAccountVC new] animated:YES];
                         }
                     },
                     @{
                         @"class":WKLabelItemModel.class,
                         @"label":LLang(@"客服列表"),
                         @"showBottomLine":@(NO),
                         @"showTopLine":@(NO),
                         @"onClick":^{
                             [[WKNavigationManager shared] pushViewController:[FFServicerListVC new] animated:YES];
                         }
                     },
             ]
        }
    ];
}

// 请求数据
- (void)requestType:(void (^)(NSError * _Nullable))complete {
    __weak typeof(self) weakSelf = self;
    [[WKAPIClient sharedClient] GET:@"pay/rechargeconfig" parameters:nil model:FFRechargeConfig.class].then(^(FFRechargeConfig *config){
        weakSelf.config = config;
        complete(nil);
    }).catch(^(NSError *error){
        complete(error);
    });
}


/// 我的钱包信息
-(void) myWallet:(void(^)(NSError *error))callback {
    __weak typeof(self) weakSelf = self;
    [[WKAPIClient sharedClient] GET:@"impay/wallet/my" parameters:nil model:FFWalletResp.class].then(^(FFWalletResp* resp){
        weakSelf.walletInfo = resp;
        if(callback) {
            callback(nil);
        }
    }).catch(^(NSError *error){
        if(callback) {
            callback(error);
        }
    });
}

-(void) ff_needEnterPayPassword:(NSString*)item callback:(void(^)(BOOL needPayPassword,NSError *error))callback {
    [[WKAPIClient sharedClient] GET:[NSString stringWithFormat:@"impay/wallet/passwordfree/items/%@/open",item] parameters:nil].then(^(NSDictionary* dict){
        NSInteger leftNum = [dict[@"left_num"] integerValue];
        BOOL open = [dict[@"open"] boolValue];
        if(open) {
            if(leftNum == -1 || leftNum>0) {
                callback(false,nil);
            }else{
                callback(true,nil);
            }
        }else{
            callback(true,nil);
        }
        
    }).catch(^(NSError *error){
        if(callback) {
            callback(false,error);
        }
    });
}

-(UIImage*) imageName:(NSString*)name {
    return [[WKApp shared] loadImage:name moduleID:@"LLWallet"];
}

@end


@implementation FFWalletResp

+ (WKModel *)fromMap:(NSDictionary *)dictory type:(ModelMapType)type {
    FFWalletResp *resp = [FFWalletResp new];
    resp.amount = [dictory[@"amount"] integerValue];
    resp.status = [dictory[@"status"] integerValue];
    resp.passwordIsSet = [dictory[@"password_is_set"] boolValue];
    return resp;
}

@end
